var express = require('express')
var path = require('path')
var bodyParser = require('body-parser')
var mongoose = require('mongoose')

const url = 'mongodb://localhost/ear';
global.db = mongoose.createConnection(url, {
	useNewUrlParser: true
});

var router = require('./router/router')

var app = express()

app.use('/public/', express.static(path.join(__dirname + './public/')))
app.use('/node_modules', express.static(path.join(__dirname + './node_modules')))

app.use(bodyParser.urlencoded({
	extended: false
}))
app.use(bodyParser.json())

app.engine('html', require('express-art-template'))

app.use(router)

app.use(function (error, req, res, next) {
	res.setHeader("Access-Control-Allow-Origin", "*");
	res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
	res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
	res.status(500).json({
		error_code: 500,
		message: error.message
	})
})

app.listen(3000, function () {
	console.log('app is running....')
})